package patrol.mobile.com.mobilepatrol.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.office.OtherOfficeDetailActivity;
import patrol.mobile.com.mobilepatrol.adapter.OtherBusinessSearchAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.OtherBusinessBean;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

import static patrol.mobile.com.mobilepatrol.activity.CommonListActivity.ROWS;

/**
 * create an instance of this fragment.
 * desc:其他业务的tab的fragment
 */
public class OtherBusinessSearchFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.app_title_rela)
    RelativeLayout app_title_rela;
    private OtherBusinessSearchAdapter mAdapter;
    private int page = 1;
    private List<OtherBusinessBean> otherBusinessBeans;
    private String mParam1;
    private String mParam2;

    public OtherBusinessSearchFragment() {
        // Required empty public constructor
    }


    public static OtherBusinessSearchFragment newInstance(String param1, String param2) {
        OtherBusinessSearchFragment fragment = new OtherBusinessSearchFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_other_business_search;
    }

    @Override
    protected void initData() {
        app_title_rela.setVisibility(View.GONE);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new OtherBusinessSearchAdapter(getActivity());
        recyclerView.setAdapter(mAdapter);
        requestData(true, "");
    }

    private void requestData(final boolean isRefresh, String title) {
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        HttpSender sender = new HttpSender(getActivity(), baseUrl, Urls.TASK_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("rows", ROWS + "");
        map.put("userManageId", current_id);
        map.put("userWorkId", current_id);
        map.put("title", title);
        map.put("is_finish", mParam1);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                //setdata
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();

                JsonArray listArray = jo.getAsJsonArray("list");
                int rows = jo.get("rows").getAsInt();
                otherBusinessBeans = GsonUtil.jsonToList(listArray.toString(), OtherBusinessBean.class);
                setData(page, isRefresh, otherBusinessBeans, rows);
            }
        });
    }

    public void setData(int page, boolean isRefresh, List data, int rows) {
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.updateItems(data);
        } else {
            int totalPage;
            if ((int) Math.ceil(rows / ROWS) == 0) {
                totalPage = 1;
            } else {
                totalPage = (int) Math.ceil(rows / ROWS);
            }
            if (page == totalPage) {
                if (page == 1) {
                    mAdapter.updateItems(data);
                    mAdapter.enableLoadMore(false);//加载loading gone掉
                } else {
                    mAdapter.addItems(data);
                    mAdapter.noMoreData();
                }
            } else {
                mAdapter.addItems(data);
//                mAdapter.enableLoadMore(false);//加载loading gone掉
                mAdapter.loadComplete();//本次加载完成
            }
        }
    }

    @Override
    protected void initListener() {
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), OtherOfficeDetailActivity.class);
                intent.putExtra(OtherOfficeDetailActivity.ID, otherBusinessBeans.get(position).getId());
                startActivity(intent);
            }
        });
    }
}
